Skip to content

Commit 7e34572

Browse files
authored
Merge pull request #6022 from emilghittasv/playwright-locator-cleanup
Playwright custom xpath cleanup
2 parents d90f77e + 7528616 commit 7e34572

26 files changed

+261
-346
lines changed

playwright_tests/pages/ask_a_question/aaq_pages/aaq_form_page.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def _get_in_progress_item_label(self) -> str:
7171
return super()._get_text_of_element(self.__in_progress_item_label)
7272

7373
def _click_on_a_particular_completed_milestone(self, milestone_name: str):
74-
xpath = f'//span[@class="progress--label" and text()="{milestone_name}"]/../..'
75-
super()._click(xpath)
74+
super()._click(f'//span[@class="progress--label" and text()="{milestone_name}"]/../..')
7675

7776
# Question subject actions.
7877
def _get_value_of_subject_input_field(self) -> str:

playwright_tests/pages/ask_a_question/contact_support_pages/contact_support_page.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ def _get_all_product_card_titles(self) -> list[str]:
3636
return super()._get_text_of_elements(self.__product_cards_titles)
3737

3838
def _get_product_card_subtitle(self, card_name: str) -> str:
39-
xpath = (f"//a[normalize-space(text()) = '{card_name}']/..//following-sibling::p["
40-
f"@class='card--desc']")
41-
return super()._get_text_of_element(xpath)
39+
return super()._get_text_of_element(f"//a[normalize-space(text()) = '{card_name}']/..//"
40+
f"following-sibling::p[@class='card--desc']")
4241

4342
def _click_on_a_particular_card(self, card_name: str):
44-
xpath = f"//h3[@class='card--title']/a[normalize-space(text())='{card_name}']"
45-
super()._click(xpath)
43+
super()._click(f"//h3[@class='card--title']/a[normalize-space(text())='{card_name}']")

playwright_tests/pages/ask_a_question/posted_question_pages/questions_page.py

Lines changed: 76 additions & 107 deletions
Large diffs are not rendered by default.

playwright_tests/pages/ask_a_question/product_solutions_pages/product_solutions_page.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def _get_current_milestone_text(self) -> str:
6565

6666
# Featured article actions.
6767
def _click_on_a_featured_article_card(self, card_name: str):
68-
xpath = f'//h2[contains(text(),"Featured Articles")]/../..//a[text()="{card_name}"]'
69-
super()._click(xpath)
68+
super()._click(f'//h2[contains(text(),"Featured Articles")]/../..//'
69+
f'a[text()="{card_name}"]')
7070

7171
def _get_all_featured_articles_titles(self) -> list[str]:
7272
return super()._get_text_of_elements(self.__featured_articles_cards)
@@ -76,9 +76,8 @@ def _is_featured_article_section_displayed(self) -> bool:
7676

7777
# Popular topic actions.
7878
def _click_on_a_popular_topic_card(self, card_name: str):
79-
xpth = (f"//h2[contains(text(),'Popular Topics')]/../..//a[normalize-space(text()) = "
80-
f"'{card_name}']")
81-
super()._click(xpth)
79+
super()._click(f"//h2[contains(text(),'Popular Topics')]/../..//"
80+
f"a[normalize-space(text()) = '{card_name}']")
8281

8382
def _get_popular_topics(self) -> list[str]:
8483
return super()._get_text_of_elements(self.__popular_topics_cards)

playwright_tests/pages/ask_a_question/product_topics_pages/product_topics_page.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,24 @@ def _get_page_title(self) -> str:
2626
return super()._get_text_of_element(self.__page_title)
2727

2828
def _get_a_particular_article_locator(self, article_title: str):
29-
xpath = f"//h2[@class='sumo-card-heading']/a[normalize-space(text())='{article_title}']"
30-
return super()._get_element_locator(xpath)
29+
return super()._get_element_locator(f"//h2[@class='sumo-card-heading']/a[normalize-space"
30+
f"(text())='{article_title}']")
3131

3232
# Navbar actions.
3333
def _get_selected_navbar_option(self) -> str:
3434
return super()._get_text_of_element(self.__selected_nav_link)
3535

3636
def _click_on_a_navbar_option(self, option_name: str):
37-
xpath = f'//a[@data-event-action="topic sidebar" and contains(text(), "{option_name}")]'
38-
super()._click(xpath)
37+
super()._click(f'//a[@data-event-action="topic sidebar" and contains(text(),'
38+
f' "{option_name}")]')
3939

4040
def _get_navbar_links_text(self) -> list[str]:
4141
return super()._get_text_of_elements(self.__navbar_links)
4242

4343
def _get_navbar_option_link(self, option_name: str) -> str:
44-
xpath = f'//a[@data-event-action="topic sidebar" and contains(text(), "{option_name}")]'
45-
return super()._get_element_attribute_value(xpath, "href")
44+
return super()._get_element_attribute_value(f'//a[@data-event-action="topic sidebar" '
45+
f'and contains(text(), "{option_name}")]',
46+
"href")
4647

4748
# AAQ section actions.
4849
def _get_aaq_subheading_text(self) -> str:

playwright_tests/pages/community_forums/forums_pages/product_support_forum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def _click_on_the_show_all_questions_option(self):
3636

3737
# Question list actions
3838
def _get_all_question_list_tags(self, question_id: str) -> list[str]:
39-
xpath = f"//article[@id='{question_id}']//li[@class='tag']"
40-
question_tags = super()._get_text_of_elements(xpath)
39+
question_tags = super()._get_text_of_elements(f"//article[@id='{question_id}']//"
40+
f"li[@class='tag']")
4141
return question_tags
4242

4343
def _extract_question_ids(self) -> list[str]:

playwright_tests/pages/community_forums/forums_pages/support_forums_page.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ def _click_on_all_products_support_forum_button(self):
2424

2525
# Product cards actions.
2626
def _click_on_a_particular_product_card(self, card_name: str):
27-
xpath = f"//strong[text()='{card_name}']"
28-
super()._click(xpath)
27+
super()._click(f"//strong[text()='{card_name}']")
2928

3029
def _get_product_card_titles_list(self) -> list[str]:
3130
return super()._get_text_of_elements(self.__product_card_titles)
3231

3332
def _get_card_description_text(self, card_title: str) -> str:
34-
xpath = f"//strong[text()='{card_title}']/../../following-sibling::p"
35-
return super()._get_text_of_element(xpath)
33+
return super()._get_text_of_element(f"//strong[text()='{card_title}']/../../"
34+
f"following-sibling::p")

playwright_tests/pages/contribute/contributor_tools_pages/article_discussions_page.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ def __init__(self, page: Page):
88
super().__init__(page)
99

1010
def _is_title_for_article_discussion_displayed(self, article_title: str) -> Locator:
11-
xpath = f"//td[@class='title']/a[text()='{article_title}']"
12-
return super()._get_element_locator(xpath)
11+
return super()._get_element_locator(f"//td[@class='title']/a[text()='{article_title}']")

playwright_tests/pages/contribute/contributor_tools_pages/kb_dashboard_page.py

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,48 @@ def __init__(self, page: Page):
1717

1818
# KB Dashboard actions
1919
def _get_a_particular_article_title_locator(self, article_name: str) -> Locator:
20-
xpath = f"//td/a[text()='{article_name}']"
21-
return super()._get_element_locator(xpath)
20+
return super()._get_element_locator(f"//td/a[text()='{article_name}']")
2221

2322
def _click_on_article_title(self, article_name: str):
24-
xpath = f"//td/a[text()='{article_name}']"
25-
super()._click(xpath)
23+
super()._click(f"//td/a[text()='{article_name}']")
2624

2725
def _get_a_particular_article_status(self, article_name: str) -> str:
28-
xpath = (f"//td/a[text()='{article_name}']/../following-sibling::td[contains(@class,"
29-
f"'status ')]")
30-
return super()._get_text_of_element(xpath)
26+
return super()._get_text_of_element(f"//td/a[text()='{article_name}']/../"
27+
f"following-sibling::td[contains(@class,'status ')]")
3128

3229
def _get_needs_update_status(self, article_name: str) -> str:
33-
xpath = (f"//td/a[text()='{article_name}']/../following-sibling::td[contains(@class,"
34-
f"'needs-update')]")
35-
return super()._get_text_of_element(xpath)
30+
return super()._get_text_of_element(f"//td/a[text()='{article_name}']/../"
31+
f"following-sibling::td[contains(@class, "
32+
f"'needs-update')]")
3633

3734
def _is_needs_change_empty(self, article_name: str) -> bool:
38-
xpath = (f"//td/a[text()='{article_name}']/../following-sibling::td[contains(@class,"
39-
f"'needs-update')]")
40-
return super()._is_element_empty(xpath)
35+
return super()._is_element_empty(f"//td/a[text()='{article_name}']/../"
36+
f"following-sibling::td[contains(@class,'needs-update')]")
4137

4238
def _get_ready_for_l10n_status(self, article_name: str) -> str:
43-
xpath = (f"//td/a[text()='{article_name}']/../following-sibling::td[contains(@class,"
44-
f"'ready-for-l10n')]")
45-
return super()._get_text_of_element(xpath)
39+
return super()._get_text_of_element(f"//td/a[text()='{article_name}']/../"
40+
f"following-sibling::td[contains"
41+
f"(@class,'ready-for-l10n')]")
4642

4743
def _get_stale_status(self, article_name: str) -> str:
48-
xpath = (f"//td/a[text()='{article_name}']/../following-sibling::td[contains(@class,"
49-
f"'stale ')]")
50-
return super()._get_text_of_element(xpath)
44+
return super()._get_text_of_element(f"//td/a[text()='{article_name}']/../"
45+
f"following-sibling::td[contains(@class,'stale ')]")
5146

5247
def _is_stale_status_empty(self, article_name: str) -> bool:
53-
xpath = (f"//td/a[text()='{article_name}']/../following-sibling::td[contains(@class,"
54-
f"'stale ')]")
55-
return super()._is_element_empty(xpath)
48+
return super()._is_element_empty(f"//td/a[text()='{article_name}']/../"
49+
f"following-sibling::td[contains(@class,'stale ')]")
5650

5751
def _get_existing_expiry_date(self, article_name: str) -> str:
58-
xpath = f"//td/a[text()='{article_name}']/../following-sibling::td/time"
59-
return super()._get_text_of_element(xpath)
52+
return super()._get_text_of_element(f"//td/a[text()='{article_name}']/../"
53+
f"following-sibling::td/time")
6054

6155
def _get_expiry_date_locator(self, article_name: str) -> Locator:
62-
xpath = f"//td/a[text()='{article_name}']/../following-sibling::td/time"
63-
return super()._get_element_locator(xpath)
56+
return super()._get_element_locator(f"//td/a[text()='{article_name}']/../"
57+
f"following-sibling::td/time")
6458

6559
def _click_on_show_translations_option(self, article_name: str):
66-
xpath = (f"//td/a[text()='{article_name}']/../following-sibling::td/a[contains(text(),"
67-
f"'Show translations')]")
68-
super()._click(xpath)
60+
super()._click(f"//td/a[text()='{article_name}']/../following-sibling::td/"
61+
f"a[contains(text(),'Show translations')]")
6962

7063
def _click_on_the_complete_overview_link(self):
7164
super()._click(self.__complete_overview_link)

playwright_tests/pages/contribute/contributor_tools_pages/media_gallery.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ def _get_image_in_documents_list_items_text(self) -> list[str]:
5252
return super()._get_text_of_elements(self.__image_in_documents_list)
5353

5454
def _click_on_a_linked_in_document(self, document_name: str):
55-
xpath = f"//div[@class='documents']//li/a[text()='{document_name}']"
56-
super()._click(xpath)
55+
super()._click(f"//div[@class='documents']//li/a[text()='{document_name}']")
5756

5857
def _click_on_delete_this_image_button(self):
5958
super()._click(self.__delete_this_image_button)
@@ -89,10 +88,9 @@ def _click_on_upload_media_button(self):
8988
super()._click(self.__upload_media_button)
9089

9190
def _select_media_file_from_list(self, media_file_name: str):
92-
xpath = f"//ol[@id='media-list']/li/a[@title='{media_file_name}']"
9391
# We need to wait a bit so that the list finishes to update in case of search.
9492
super()._wait_for_given_timeout(1000)
95-
super()._click(xpath)
93+
super()._click(f"//ol[@id='media-list']/li/a[@title='{media_file_name}']")
9694

9795
def _click_on_insert_media_button(self):
9896
super()._click(self.__insert_media_button)

playwright_tests/pages/contribute/contributor_tools_pages/moderate_forum_content.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,48 @@ def __init__(self, page: Page):
1111
super().__init__(page)
1212

1313
def _get_flagged_question_locator(self, question_title) -> Locator:
14-
xpath = f"//p[text()='{question_title}']"
15-
return super()._get_element_locator(xpath)
14+
return super()._get_element_locator(f"//p[text()='{question_title}']")
1615

1716
def _get_flag_reason(self, question_title: str) -> str:
18-
xpath = f"//p[text()='{question_title}']/../preceding-sibling::hgroup/h2"
19-
return super()._get_text_of_element(xpath)
17+
return super()._get_text_of_element(f"//p[text()='{question_title}']/../"
18+
f"preceding-sibling::hgroup/h2")
2019

2120
def _get_flagged_question_content(self, question_title: str) -> str:
22-
xpath = f"//p[text()='{question_title}']/following-sibling::div[@class='content']/p"
23-
return super()._get_text_of_element(xpath)
21+
return super()._get_text_of_element(f"//p[text()='{question_title}']/"
22+
f"following-sibling::div[@class='content']/p")
2423

2524
def _get_created_by_link_text(self, question_title: str) -> str:
26-
xpath = f"//p[text()='{question_title}']/following-sibling::p[@class='created']/a"
27-
return super()._get_text_of_element(xpath)
25+
return super()._get_text_of_element(f"//p[text()='{question_title}']/"
26+
f"following-sibling::p[@class='created']/a")
2827

2928
def _click_created_by_link(self, question_title: str):
30-
xpath = f"//p[text()='{question_title}']/following-sibling::p[@class='created']/a"
31-
super()._click(xpath)
29+
super()._click(f"//p[text()='{question_title}']/"
30+
f"following-sibling::p[@class='created']/a")
3231

3332
def _get_flagged_by_link_text(self, question_title: str) -> str:
34-
xpath = f"//p[text()='{question_title}']/following-sibling::p[@class='flagged']/a"
35-
return super()._get_text_of_element(xpath)
33+
return super()._get_text_of_element(f"//p[text()='{question_title}']/"
34+
f"following-sibling::p[@class='flagged']/a")
3635

3736
def _click_flagged_by_link(self, question_title: str):
38-
xpath = f"//p[text()='{question_title}']/following-sibling::p[@class='flagged']/a"
39-
super()._click(xpath)
37+
super()._click(f"//p[text()='{question_title}']/"
38+
f"following-sibling::p[@class='flagged']/a")
4039

4140
def _click_take_action_view_option(self, question_title: str):
42-
xpath = f"//p[text()='{question_title}']/following-sibling::div/a[text()='View']"
43-
super()._click(xpath)
41+
super()._click(f"//p[text()='{question_title}']/following-sibling::div/a[text()='View']")
4442

4543
def _click_take_action_edit_option(self, question_title: str):
46-
xpath = f"//p[text()='{question_title}']/following-sibling::div/a[text()='Edit']"
47-
super()._click(xpath)
44+
super()._click(f"//p[text()='{question_title}']/following-sibling::div/a[text()='Edit']")
4845

4946
def _click_take_action_delete_option(self, question_title: str):
50-
xpath = f"//p[text()='{question_title}']/following-sibling::div/a[text()='Delete']"
51-
super()._click(xpath)
47+
super()._click(f"//p[text()='{question_title}']/following-sibling::div/a[text()='Delete']")
5248

5349
def _select_update_status_option(self, question_title: str, select_value: str):
54-
xpath = f"//p[text()='{question_title}']/following-sibling::form/select"
55-
super()._select_option_by_label(xpath, select_value)
50+
super()._select_option_by_label(f"//p[text()='{question_title}']/"
51+
f"following-sibling::form/select", select_value)
5652

5753
def _click_on_the_update_button(self, questions_title: str):
58-
xpath = f"//p[text()='{questions_title}']/following-sibling::form/input[@value='Update']"
59-
super()._click(xpath)
54+
super()._click(f"//p[text()='{questions_title}']/following-sibling::form/"
55+
f"input[@value='Update']")
6056

6157
def _click_view_all_deactivated_users_button(self):
6258
super()._click(self.__view_all_deactivated_users_button)

0 commit comments

Comments
 (0)